﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace QuanLySinhVien_Report.DataPlayer
{
    public class DatasetPlayer
    {
        private DataTable dt;
        private SqlConnection con;
        private SqlDataAdapter da;
        public void KETNOI()
        {
            string sql = @"Data Source=HOANGBACH;Initial Catalog=QLSV;Integrated Security=True";
            try
            {
                con = new SqlConnection(sql);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Loi :" + ex.Message);
            }
        }
        public DataTable GetDatatable(string s)
        {
            KETNOI();
            if (con.State == ConnectionState.Closed)
                con.Open();
            da = new SqlDataAdapter(s, con);
            dt = new DataTable();
            da.Fill(dt);
            if (con.State == ConnectionState.Open)
                con.Close();
            return dt;
        }
        public bool ChayQuery(string s)
        {
            int kt = 0;
            try
            {
                if (con.State == ConnectionState.Closed)
                    con.Open();
                SqlCommand cmd = new SqlCommand(s, con);
                kt = cmd.ExecuteNonQuery();
                if (con.State == ConnectionState.Open)
                    con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Loi " + ex.Message);
            }
            if (kt > 0)
                return true;
            return false;
        }
        // Kiem tra du lieu co tren bang ko neu co tra va true nguoc lai tra ve false

        public bool kiemtra(string nametable, string namefield, string value)
        {
            string sql = "select * from " + nametable + " where " + namefield + "='" + value + "'";
            GetDatatable(sql);
            if (dt.Rows.Count > 0)
                return true;
            return false;
        }
        public bool kiemtrahaifield(string tbname, string field1, string field2, string val1, string val2)
        {
            string sql = "select * from " + tbname + " where " + field1 + "='" + val1 + "' and " + field2 + "='" + val2 + "'";
            GetDatatable(sql);
            if (dt.Rows.Count > 0)
                return true;
            return false;
        }
    }
}
